Abstract classes in C#

In the definition of class, the abstract abstract, marked by the keyword abstract, is usually used to define the base class in the hierarchy, what is special about them, is that you can not make an example in them - if you If you try, you will get a compilation error. Instead, you have to divide them, as taught in the chapter of heritage, and when you need an abstract class, do you make an example of your subclass? It really depends on you

Honestly, you can go a long way without the need for an abstract class, but they are great for specific things, such as frameworks, that is why you will find some abstract classes within the .NET framework. . A good rule of thumb is that the name really feels great - intangible classes are often, if used as usual, to describe some essence, which is more of a concept than a real thing.

In this example, we make a base class for four-legged animals and then make a dog class, which is obtained in this way:


namespace AbstractClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Console.WriteLine(dog.Describe());
            Console.ReadKey();
        }
    }

    abstract class FourLeggedAnimal
    {
        public virtual string Describe()
        {
            return "Not much is known about this four legged animal!";
        }
    }

    class Dog : FourLeggedAnimal
    {

    }
}

If you compare examples of chapter of heritage, you will not see a big difference. Actually, the key word is the biggest difference in front of four-footed acid definitions, as you can see, we make a new example of the dog class and then call the details () inherited from the four-footed animal classes. Now try to make an example of four legional classes instead:


FourLeggedAnimal someAnimal = new FourLeggedAnimal();

You will get this fine compiler error: 
Cannot create an instance of the abstract class or interface 'AbstractClasses.FourLeggedAnimal' 
Now, as you can see, we just inherited the Describe() method, but it isn't very useful in it's current form, for our Dog class. Let's override it:


class Dog : FourLeggedAnimal
{
    public override string Describe()
    {
        return "This four legged animal is a Dog!";
    }
}

In this case, we do a complete override, but in some cases, you might want to use the behavior from the base class in addition to new functionality. This can be done by using the base keyword, which refers to the class we inherit from:


abstract class FourLeggedAnimal
{
    public virtual string Describe()
    {
        return "This animal has four legs.";
    }
}


class Dog : FourLeggedAnimal
{
    public override string Describe()
    {
        string result = base.Describe();
        result += " In fact, it's a dog!";
        return result;
    }
}

Now obviously, you can create other subclasses of the FourLeggedAnimal class - perhaps a cat or a lion? In the next chapter, we will do a more advanced example and introduce abstract methods as well. Read on.